/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package APP2.bookshop.controller;

import APP2.bookshop.main.BookShop;
import APP2.bookshop.order.Order;
import APP2.bookshop.order.OrderInfo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;

/**
 *
 * @author finalfire
 */
public class OrderController {

    private BookShop bookshop;
    private DateFormat format;

    public OrderController(BookShop bookshop) {
        this.format = new SimpleDateFormat("dd/MM/yyyy");
        this.bookshop = bookshop;
    }

    public OrderInfo getOrderByCode(int code) {
        for (OrderInfo o : this.bookshop.getOrders())
            if (o.getCode() == code)
                return o;
        return null;
    }

    // Return an AbstractListModel for JList component
    public javax.swing.AbstractListModel getOrdersModel() {
        // create a new array to save authors' name
        //final String[] elements = new String[this.bookshop.getPublishers().size()];
        //for (int i = 0; i < elements.length; i++)
            //elements[i] = this.bookshop.getPublishers().get(i).getName();

        // return a new AbstractListModel
        return UtilController.getListModelFromArray(this.getOrdersArray());
    }

    public javax.swing.table.DefaultTableModel getContentModel(int code) {
        Vector<String> cols = new Vector<String>();
        cols.add("Book");
        cols.add("Copies");

        Vector<Vector<String>> rows = new Vector<Vector<String>>();
        Vector<String> temp;
        for (Order ord : this.getOrderByCode(code).getOrder()) {
            temp = new Vector<String>();
            temp.add(ord.getBook().getTitle());
            temp.add(Integer.toString(ord.getCopy()));
            rows.add(temp);
        }
        
        return new javax.swing.table.DefaultTableModel(rows, cols);

    }

    private String[] getOrdersArray() {
        String[] orders = new String[this.bookshop.getOrders().size()];
        for (int i = 0; i < orders.length; i++)
            orders[i] = Integer.toString(this.bookshop.getOrders().get(i).getCode()) + "#" +
                        this.format.format(this.bookshop.getOrders().get(i).getOrderDate());
        return orders;
    }
}
